<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
	<head>
		<jsp:include page="../inc.jsp"></jsp:include>
		<script type="text/javascript" src="../My97DatePicker/WdatePicker.js"></script>
		<script type="text/javascript" charset="utf-8">
	var datagrid;
	var auth = "${auths}";
	var buttons = [];
	$(function() {
		if(auth=="all"){
			buttons.push({text : '新增盘点',iconCls : 'icon-add',handler : function() {append();}});
			buttons.push('-');
			buttons.push({text : '继续盘点',iconCls : 'icon-edit',handler : function() {edit();}});
			buttons.push('-');
			buttons.push({text : '删除盘点',iconCls : 'icon-remove',handler : function() {remove();}});
			buttons.push('-');
			buttons.push({text : '取消选中',iconCls : 'icon-undo',handler : function() {datagrid.datagrid('clearSelections');datagrid.datagrid('unselectAll');}});
			buttons.push('-');
		}else if(auth!=""){
			var auths = auth.split(",");
			if(auths.length>0){
				for(var i=0;i<auths.length;i++){
					if(auths[i]=="KCGL_KCPD_ADD"){
						buttons.push({text : '新增盘点',iconCls : 'icon-add',handler : function() {append();}});
						buttons.push('-');
					}else if(auths[i]=="KCGL_KCPD_CONTINUE"){
						buttons.push({text : '继续盘点',iconCls : 'icon-edit',handler : function() {edit();}});
						buttons.push('-');
					}else if(auths[i]=="KCGL_KCPD_DELETE"){
						buttons.push({text : '删除盘点',iconCls : 'icon-remove',handler : function() {remove();}});
						buttons.push('-');
					}
				}
				buttons.push({text : '取消选中',iconCls : 'icon-undo',handler : function() {datagrid.datagrid('clearSelections');datagrid.datagrid('unselectAll');}});
				buttons.push('-');
			}
		}
		datagrid = $('#datagrid').datagrid({
			url : 'goodsCheckAction!datagrid.html',
			title : '库存盘点列表',
			iconCls : 'icon-save',
			pagination : true,
			pagePosition : 'bottom',
			pageSize : 10,
			pageList : [ 10, 20, 30, 40 ],
			fit : true,
			fitColumns : false,
			nowrap : false,
			border : false,
			idField : 'id',
			sortName : 'serial',
			sortOrder : 'desc',
			checkOnSelect : false,
			selectOnCheck : true,
			frozenColumns : [ [ {
				title : '编号',
				field : 'id',
				width : 150,
				sortable : true,
				checkbox : true
			}] ],
			columns : [ [{
				title : '仓库ID',
				field : 'rid',
				width : 100,
				hidden : true
			}, {
				title : '盘点仓库',
				field : 'rname',
				width : 150
			},  {
				title : '日期',
				field : 'date',
				width : 150,
				formatter : function(value, row, index) {
					return value.substring(0,10);
				}
			}, {
				title : '单号',
				field : 'serial',
				width : 150
			},{
				title : '操作员',
				field : 'uname',
				width : 150
			}, {
				title : '盘点状态',
				field : 'isCheckOver',
				width : 100,
				formatter : function(value, row, index) {
					if(value){
						return '已盘完';
					}else{
						return '未盘完';
					}
				}
			},{
				title : '备注',
				field : 'remark',
				width : 150
			} ] ],
			toolbar : buttons
		});
		
		var rid = $('#searchForm').find('input[name=rid]');
		var ridCombobox = rid.combobox({
			url : '${pageContext.request.contextPath}/rkgl/repertoryAction!noAuth_combobox.html',
			valueField : 'id',
			textField : 'name',
			multiple : false,
			editable : false,
			panelHeight : 'auto'
		});
		
		var isCheckOver = $('#searchForm').find('input[name=isCheckOver]');
		var isCheckOverCombobox = isCheckOver.combobox({
			data : [{isCheckOver:'0',text:'未盘完'},{isCheckOver:'1',text:'已盘完'}],
			valueField : 'isCheckOver',
			textField : 'text',
			multiple : false,
			editable : false,
			panelHeight : 'auto'
		});
	});
	
	function edit() {
		var rows = datagrid.datagrid('getSelections');
		if (rows.length == 1) {
			if(rows[0].isCheckOver){
				parent.sy.messagerShow({
					msg : "您勾选的项目已盘完，无须继续盘点！",
					title : '提示'
				});
				return;
			}else{
				var id = rows[0].id;
				var node = {id:'KCGL_KCPD_XG',text:'修改库存盘点单',state:'open',attributes:{url:'/kcgl/goodsCheckItemAction!goodsCheckItemEdit.html?id='+id}};
				if (parent.centerTabs.tabs('exists', node.text)) {
					parent.centerTabs.tabs('select', node.text);
					parent.refreshTab(node.text);
				}else{
					parent.addNoRefreshTab(node);
				}
			}
		} else if (rows.length > 1) {
			parent.sy.messagerAlert('提示', '同一时间只能盘点一条记录！', 'error');
		} else {
			parent.sy.messagerAlert('提示', '请选择要盘点的记录！', 'error');
		}
	}
	
	function append() {
		var p = parent.sy.dialog({
			title : '确定盘点仓库',
			href : '${pageContext.request.contextPath}/kcgl/goodsCheckItemAction!noAuth_goodsCheckRepertory.html',
			width : 440,
			height : 110,
			buttons : [ {
				text : '确定',
				handler : function() {
					var f = p.find('form');
					if(!f.form("validate"))
						return;
					var rid = p.find("input[name='rid']").eq(0).val();
					
					var hasUnCheckOver = false;
					var lastId = null;
					var rows = datagrid.datagrid('getRows');
					for ( var i = 0; i < rows.length; i++) {
						if(!rows[i].isCheckOver&&rows[i].rid == rid){
							hasUnCheckOver = true;
							lastId = rows[i].id;
							break;
						}
					}
					if(hasUnCheckOver){
						parent.sy.messagerConfirm('请确认', '您选择的仓库还有未盘完的盘点单，无法新增。是否继续上一次盘点？', function(r) {
							if (r) {
								var node = {id:'KCGL_KCPD_XG',text:'修改库存盘点单',state:'open',attributes:{url:'/kcgl/goodsCheckItemAction!goodsCheckItemEdit.html?id='+lastId+'&rid='+rid}};
								if (parent.centerTabs.tabs('exists', node.text)) {
									parent.centerTabs.tabs('select', node.text);
									parent.refreshTab(node.text);
								}else{
									parent.addNoRefreshTab(node);
								}
								p.dialog('close');
							}
						});
					}else{
						var node = {id:'KCGL_KCPD_XZ',text:'新增库存盘点单',state:'open',attributes:{url:'/kcgl/goodsCheckItemAction!goodsCheckItemAdd.html?rid='+rid}};
						if (parent.centerTabs.tabs('exists', node.text)) {
							parent.centerTabs.tabs('select', node.text);
							parent.refreshTab(node.text);
						}else{
							parent.addNoRefreshTab(node);
						}
						p.dialog('close');
					}
				}
			} ],
			onLoad : function() {
				var f = p.find('form');
				var rid = f.find('input[name=rid]');
				var ridCombobox = rid.combobox({
					url : '${pageContext.request.contextPath}/rkgl/repertoryAction!noAuth_usedCombobox.html',
					valueField : 'id',
					textField : 'name',
					multiple : false,
					editable : false,
					panelHeight : 'auto'
				});
			}
		});
	}
	
	function remove() {
		var rows = datagrid.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			parent.sy.messagerConfirm('请确认', '您要删除当前所选项目？', function(r) {
				if (r) {
					for ( var i = 0; i < rows.length; i++) {
						if(rows[i].isCheckOver){
							parent.sy.messagerShow({
								msg : "您勾选的项目中包含已盘点项，无法删除！",
								title : '提示'
							});
							return;
						}
						ids.push(rows[i].id);
					}
					$.ajax({
						url : '${pageContext.request.contextPath}/kcgl/goodsCheckAction!delete.html',
						data : {
							ids : ids.join(',')
						},
						dataType : 'json',
						success : function(d) {
							datagrid.datagrid('load');
							datagrid.datagrid('unselectAll');
							parent.sy.messagerShow({
								title : '提示',
								msg : d.msg
							});
						}
					});
				}
			});
		} else {
			parent.sy.messagerAlert('提示', '请勾选要删除的记录！', 'error');
		}
	}

	function _search() {
		datagrid.datagrid('load', sy.serializeObject($('#searchForm')));
	}
	
	function cleanSearch() {
		datagrid.datagrid('load', {});
		$('#searchForm input').val('');
	}
</script>
	</head>
	<body class="easyui-layout" data-options="fit:true">

		<div data-options="region:'north',border:false,title:'搜索条件'"
			style="height: 120px; overflow: hidden;">
			<form id="searchForm">
				<table class="tableForm datagrid-toolbar"
					style="width: 100%; height: 100%;">
					<tr>
						<th>
							当前仓库
						</th>
						<td>
							<input name="rid" style="width: 200px;" />
						</td>
						<th>
							盘点状态
						</th>
						<td>
							<input name="isCheckOver" style="width: 200px;" />
						</td>
					</tr>
					<tr>
						<th style="width: 80px;">
							开始日期
						</th>
						<td style="width: 200px;">
							<input name="beginDate" onClick="WdatePicker()" class="Wdate" style="width: 200px;" />
						</td>
						<th style="width: 80px;">
							截止日期
						</th>
						<td style="width: 200px;">
							<input name="endDate" onClick="WdatePicker()" class="Wdate" style="width: 200px;" />
						</td>
					</tr>
					<tr>
						<td colspan="6" align="right" style="text-align: center">
							<a href="javascript:void(0);" class="easyui-linkbutton" onclick="_search();">搜索</a>
							<a href="javascript:void(0);" class="easyui-linkbutton" onclick="cleanSearch();">取消</a>
						</td>
					</tr>
				</table>
			</form>
		</div>

		<div data-options="region:'center',border:false"
			style="overflow: hidden;">
			<table id="datagrid"></table>
		</div>

		<div id="menu" class="easyui-menu" style="width: 120px; display: none;">
			<k:security key="KCGL_KCPD_ADD">
				<div onclick="append();" data-options="iconCls:'icon-add'">新增盘点</div>
			</k:security>
			<k:security key="KCGL_KCPD_CONTINUE">
				<div onclick="remove();" data-options="iconCls:'icon-remove'">继续盘点</div>
			</k:security>
			<k:security key="KCGL_KCPD_DELETE">
				<div onclick="edit();" data-options="iconCls:'icon-edit'">删除盘点</div>
			</k:security>
		</div>
	</body>
</html>